package com.xsd.xsdhouduan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xsd.xsdhouduan.entity.School;
import com.xsd.xsdhouduan.entity.User;
import com.xsd.xsdhouduan.mapper.UserMapper;
import com.xsd.xsdhouduan.service.SchoolService;
import com.xsd.xsdhouduan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    @Autowired
    private SchoolService schoolService;
    
    @Override
    public User login(String studentId, String password) {
        User user = getByStudentId(studentId);
        if (user != null && password.equals(user.getPassword())) {
            return user;
        }
        return null;
    }
    
    @Override
    public boolean register(User user) {
        // 检查学号是否已存在
        if (getByStudentId(user.getStudentId()) != null) {
            return false;
        }
        
        // 不加密密码，直接保存明文
        // user.setPassword(passwordEncoder.encode(user.getPassword()));
        
        // 设置默认值
        user.setBalance(0.0);
        user.setStatus(1);
        
        return save(user);
    }
    
    @Override
    public User getByStudentId(String studentId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getStudentId, studentId);
        return getOne(wrapper);
    }
    
    @Override
    public boolean updateBalance(Long userId, Double amount) {
        User user = getById(userId);
        if (user != null) {
            user.setBalance(user.getBalance() + amount);
            return updateById(user);
        }
        return false;
    }
    
    @Override
    public boolean addBalance(Long userId, java.math.BigDecimal amount) {
        User user = getById(userId);
        if (user != null) {
            user.setBalance(user.getBalance() + amount.doubleValue());
            return updateById(user);
        }
        return false;
    }
    
    @Override
    public List<User> getRecentUsers(int limit) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(User::getCreateTime);
        wrapper.last("LIMIT " + limit);
        List<User> users = list(wrapper);
        return users.stream().map(this::populateUserDetails).collect(java.util.stream.Collectors.toList());
    }
    
    @Override
    public List<User> getAllUsersWithDetails() {
        List<User> users = list();
        return users.stream().map(this::populateUserDetails).collect(java.util.stream.Collectors.toList());
    }
    
    /**
     * 填充用户详情（学校名称等）
     */
    private User populateUserDetails(User user) {
        // 填充学校名称
        if (user.getSchoolId() != null) {
            School school = schoolService.getById(user.getSchoolId());
            if (school != null) {
                user.setSchoolName(school.getName());
            }
        }
        return user;
    }
}
